package com.ruoyi.fly.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.fly.domain.*;
import com.ruoyi.fly.domain.vo.ProductInsertVo;
import com.ruoyi.fly.domain.vo.ProductSearchVo;
import com.ruoyi.fly.domain.vo.ProductVo;
import com.ruoyi.fly.domain.vo.StoreProductServerVo;
import com.ruoyi.fly.mapper.*;
import com.ruoyi.fly.service.ProductService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductCategoryMapper productCategoryMapper;

    @Autowired
    private AppletStoreProductMapper appletStoreProductMapper;

    @Autowired
    private AppletStoreServerMapper appletStoreServerMapper;

    @Autowired
    private AppletServerMapper appletServerMapper;

    @Autowired
    private AppletVehiclesProductMapper appletVehiclesProductMapper;

    /**
     * 根据商品主键查询商品
     *
     * @param id 商品主键
     * @return
     */
    @Override
    public Product selectProductById(String id) {
        Product product = productMapper.selectProductById(id);
        ProductVo productVo = new ProductVo();
        BeanUtils.copyProperties(product, productVo);
        ProductCategory productCategory = productCategoryMapper.selectProductCategoryById(String.valueOf(productVo.getCategoryId()));
        if (productCategory != null) {
            productVo.setCategoryName(productCategory.getCategoryName());
        }
        return product;
    }

    /**
     * 查询商品列表
     *
     * @param productSearchVo 商品
     * @return
     */
    @Override
    public List<ProductVo> selectProductList(ProductSearchVo productSearchVo) {
        Product product = new Product();
        BeanUtils.copyProperties(productSearchVo, product);
        List<ProductVo> productVos = productMapper.selectProductList(product);
        for (ProductVo productVo : productVos) {
            ProductCategory productCategory = productCategoryMapper.selectProductCategoryById(String.valueOf(productVo.getCategoryId()));
            if (productCategory != null) {
                productVo.setCategoryName(productCategory.getCategoryName());
            }
        }
        return productVos;
    }

    /**
     * 新增商品
     *
     * @param product 商品
     * @return
     */
    @Override
    public R insertProduct(Product product) {
        product.setCreateUser(String.valueOf(SecurityUtils.getUserId()));
        return productMapper.insertProduct(product) > 0 ? R.ok("新增成功") : R.fail("操作失败");
    }

    /**
     * 上架商品
     *
     * @param productVo
     * @return
     */
    @Override
    public R onlineProduct(ProductVo productVo) {
        return productMapper.onlineProduct(productVo.getId()) > 0 ? R.ok("上架成功") : R.fail("操作失败");
    }

    /**
     * 下架商品
     *
     * @param productVo
     * @return
     */
    @Override
    public R offlineProduct(ProductVo productVo) {
        return productMapper.offlineProduct(productVo.getId()) > 0 ? R.ok("下架成功") : R.fail("操作失败");
    }

    /**
     * 修改商品
     *
     * @param product 商品
     * @return
     */
    @Override
    public R updateProduct(Product product) {
        product.setUpdateUser(String.valueOf(SecurityUtils.getUserId()));
        return productMapper.updateProduct(product) > 0 ? R.ok("修改成功") : R.fail("操作失败");
    }

    /**
     * 删除商品
     *
     * @param id 商品主键
     * @return
     */
    @Override
    public R deletesProductById(String id) {
        return productMapper.deleteProductById(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    @Override
    public R deleteProductByIds(String[] id) {
        return productMapper.deleteProductByIds(id) > 0 ? R.ok("删除成功") : R.fail("操作失败");
    }

    /**
     * 新增店铺商品或服务
     *
     * @param productInsertVo
     * @return
     */
    @Override
    public R insertStoreProduct(ProductInsertVo productInsertVo) {
        if (productInsertVo.getCategoryType() == 0) {
            Product product = productMapper.selectProductById(productInsertVo.getProductId());
            AppletStoreProduct appletStoreProduct = new AppletStoreProduct();
            appletStoreProduct.setStoreId(productInsertVo.getStoreId());
            appletStoreProduct.setProductId(product.getId());
            appletStoreProduct.setStorePrice(product.getStorePrice());
            appletStoreProduct.setGroupBuyPrice(product.getStorePrice());
            appletStoreProduct.setCreateUser(SecurityUtils.getUserId().toString());
            if (appletStoreProductMapper.selectStoreProductByProductId(productInsertVo.getProductId(), productInsertVo.getStoreId()) != null) {
                return R.fail("该商品已添加");
            } else {
                appletStoreProductMapper.insert(appletStoreProduct);
                return R.ok("添加成功");
            }
        } else if (productInsertVo.getCategoryType() == 1) {
            AppletServer appletServer = appletServerMapper.selectAppletServerById(productInsertVo.getProductId());
            AppletStoreServer appletStoreServer = new AppletStoreServer();
            appletStoreServer.setStoreId(productInsertVo.getStoreId());
            appletStoreServer.setServerId(appletServer.getId());
            appletStoreServer.setStorePrice(appletServer.getStorePrice());
            appletStoreServer.setGroupBuyPrice(appletServer.getStorePrice());
            appletStoreServer.setCreateUser(SecurityUtils.getUserId().toString());
            if (appletStoreServerMapper.selectStoreServerById(productInsertVo.getProductId(), productInsertVo.getStoreId()) != null) {
                return R.fail("该服务已添加");
            } else {
                appletStoreServerMapper.insertStoreServer(appletStoreServer);
                return R.ok("添加成功");
            }
        }
        return R.fail("添加失败");
    }

    /**
     * 查询店铺商品或服务
     */
    @Override
    public R selectStoreProductServerList(StoreProductServerVo storeProductServerVo) {
        if (storeProductServerVo.getStoreOrVehiclesId().length()==19){
            PageHelper.startPage(storeProductServerVo.getPageNum(), storeProductServerVo.getPageSize());
            List<StoreProductServer> storeProductServers = appletStoreProductMapper.selectStoreProductServerList(storeProductServerVo);
            PageInfo<StoreProductServer> pageInfo = new PageInfo<>(storeProductServers);
            return R.ok(pageInfo);
        }else {
            PageHelper.startPage(storeProductServerVo.getPageNum(), storeProductServerVo.getPageSize());
            List<StoreProductServer> storeProductServers = appletVehiclesProductMapper.selectVehiclesProductServerList(storeProductServerVo);
            PageInfo<StoreProductServer> pageInfo = new PageInfo<>(storeProductServers);
            return R.ok(pageInfo);
        }
    }


}
